package com.zyzd.general.module.recyclerview.textfragment;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zyzd.general.R;
import com.zyzd.general.entity.local.TextEntity;
import com.zyzd.retrofit.base.BaseAdapter;

import java.util.List;

/**
 * Created by lzy on 2018/6/4 0004.
 * description ${文本列表Adapter}
 */
class TextAdapter extends BaseAdapter<TextAdapter.TextHolder, TextEntity> {


    public TextAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public TextHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TextHolder(mInflater.inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindHolder(@NonNull TextHolder holder, int position) {
        TextEntity item = getItem(position);

        switch (getItemViewType(position)) {
            case TextEntity.TYPE_LEFT:
                holder.tvContent.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                holder.tvContent.setText(String.format("left:%s", item.getContent()));
                break;
            case TextEntity.TYPE_RIGHT:
                holder.tvContent.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                holder.tvContent.setText(String.format("right:%s", item.getContent()));
                break;
            case TextEntity.TYPE_LINE:
            default:
                holder.tvContent.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                holder.tvContent.setText(String.format("line:%s", item.getContent()));
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    switch (getItemViewType(position)) {
                        case TextEntity.TYPE_LEFT:
                        case TextEntity.TYPE_RIGHT:
                            return 1;
                        case TextEntity.TYPE_LINE:
                        default:
                            return 2;
                    }
                }
            });
        }
    }

    @Override
    public void addAll(boolean isClear, List<TextEntity> list) {
        super.addAll(isClear, list);
        handleData();//处理数据
    }

    int maxLengh = 10;

    private void handleData() {

        for (int i = 0; i < mData.size(); i++) {
            TextEntity textEntity = mData.get(i);

            if (i == 0 || mData.get(i - 1).getType() == TextEntity.TYPE_LINE || mData.get(i - 1).getType() == TextEntity.TYPE_RIGHT) {
                textEntity.setType(textEntity.getContent().length() > maxLengh ? TextEntity.TYPE_LINE : TextEntity.TYPE_LEFT);
            } else {
                textEntity.setType(textEntity.getContent().length() > maxLengh ? TextEntity.TYPE_LINE : TextEntity.TYPE_RIGHT);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    static class TextHolder extends RecyclerView.ViewHolder {
        final TextView tvContent;

        TextHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }
}
